package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.DailySalesReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.DailySalesReportForm;
import com.youlai.boot.system.model.query.DailySalesReportQuery;
import com.youlai.boot.system.model.vo.DailySalesReportVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
import com.youlai.boot.common.excel.ExcelOperationService;
import com.youlai.boot.common.result.ExcelResult;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;

/**
 * TP运营日报前端控制层
 *
 * @author xiheforever
 * @since 2025-05-13 10:02
 */
@Tag(name = "TP运营日报接口")
@RestController
@RequestMapping("/api/v1/daily-sales-report")
@RequiredArgsConstructor
public class DailySalesReportController  {

    private final DailySalesReportService dailySalesReportService;
    private final ExcelOperationService excelOperationService;

    @Operation(summary = "TP运营日报分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:query')")
    public PageResult<DailySalesReportVO> getDailySalesReportPage(DailySalesReportQuery queryParams ) {
        IPage<DailySalesReportVO> result = dailySalesReportService.getDailySalesReportPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "获取可用日期列表")
    @GetMapping("/available-dates")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:query')")
    public Result<List<String>> getAvailableDates() {
        List<String> dates = dailySalesReportService.getAvailableDates();
        return Result.success(dates);
    }

    @Operation(summary = "根据日期获取日报详情")
    @GetMapping("/by-date")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:query')")
    public Result<DailySalesReportVO> getDailySalesReportByDate(
        @Parameter(description = "报告日期，格式：YYYY-MM-DD") @RequestParam String date
    ) {
        DailySalesReportVO reportData = dailySalesReportService.getDailySalesReportByDate(date);
        return Result.success(reportData);
    }

    @Operation(summary = "新增TP运营日报")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:add')")
    public Result<Void> saveDailySalesReport(@RequestBody @Valid DailySalesReportForm formData ) {
        boolean result = dailySalesReportService.saveDailySalesReport(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取TP运营日报表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:edit')")
    public Result<DailySalesReportForm> getDailySalesReportForm(
        @Parameter(description = "TP运营日报ID") @PathVariable Long id
    ) {
        DailySalesReportForm formData = dailySalesReportService.getDailySalesReportFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改TP运营日报")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:edit')")
    public Result<Void> updateDailySalesReport(
            @Parameter(description = "TP运营日报ID") @PathVariable Long id,
            @RequestBody @Validated DailySalesReportForm formData
    ) {
        boolean result = dailySalesReportService.updateDailySalesReport(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除TP运营日报")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:delete')")
    public Result<Void> deleteDailySalesReports(
        @Parameter(description = "TP运营日报ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = dailySalesReportService.deleteDailySalesReports(ids);
        return Result.judge(result);
    }

    /**
     * 导出日报数据Excel
     *
     * @param queryParams 查询参数
     * @param response HTTP响应
     */
    @Operation(summary = "导出日报数据")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:query')")
    public void exportExcel(DailySalesReportQuery queryParams, HttpServletResponse response) {
        excelOperationService.exportExcel(queryParams, "daily-sales-report", response);
    }

    /**
     * 导入日报数据Excel
     *
     * @param file Excel文件
     * @return 导入结果
     */
    @Operation(summary = "导入日报数据")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:add')")
    public Result<ExcelResult> importExcel(@Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file) {
        ExcelResult result = excelOperationService.importExcel(file, "daily-sales-report");
        return Result.success(result);
    }

    /**
     * 下载日报数据导入模板
     *
     * @param response HTTP响应
     */
    @Operation(summary = "下载导入模板")
    @GetMapping("/template")
    @PreAuthorize("@ss.hasPerm('system:daily-sales-report:query')")
    public void downloadTemplate(HttpServletResponse response) {
        excelOperationService.downloadTemplate("daily-sales-report", response);
    }
}
